package durose.modular.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import durose.system.dict.CommonDict;
import durose.system.pojo.Boss;
 import durose.system.service.BossService; 

import durose.system.pojo.Dept;
 import durose.system.service.DeptService; 


import durose.system.pojo.Empl; 
import durose.system.service.EmplService;
 import durose.system.springUtil.SpringEmplFactory; 

import durose.system.dict.ResponseDict;

@Controller
public class EmplController {
	String prefix = "system/modular/empl/";
	
	@Autowired 
 	 BossService bossService;
@Autowired 
 	 DeptService deptService;

	
	@Autowired
	EmplService emplService;
	
	
	@RequestMapping("empl")
	public String emplGet(){
		return prefix+"empl";
	}
	
	@RequestMapping("empl_list")
	@ResponseBody
	public Object emplList(@RequestParam(value="pageNumber",defaultValue="0")Integer pagenumber,@RequestParam(value="queryName",required=false) String queryName){
		PageHelper.startPage(pagenumber,CommonDict.PAGE_SIZE);
		
		
		List<Empl> emples = emplService.emplList(queryName);
		//把从数据库中查询的pojo数据与所关联的表进行处理
		List<Map<String,Object>> mapList = SpringEmplFactory.me().initData(emples);
		PageInfo<Map<String,Object>> page = new PageInfo<>(mapList);
		return page;
	}
	
	@RequestMapping("empl_add")
	@ResponseBody
	public Object emplAdd(Empl pojo){
		emplService.emplAdd(pojo);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	@RequestMapping("empl_add_page")
	public String emplAddPage(Model model){
		model.addAttribute("bosss", bossService.bossList(""));
model.addAttribute("depts", deptService.deptList(""));

		return prefix+"empl_add";
	}
	
	@RequestMapping("empl_edit")
	@ResponseBody
	public Object emplEdit(Empl pojo){
		emplService.emplUpdate(pojo);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	@RequestMapping("empl_edit_page")
	public String  emplEditPage(int id,Model model){
		model.addAttribute("bosss", bossService.bossList(""));
model.addAttribute("depts", deptService.deptList(""));

		model.addAttribute("empl", emplService.emplGet(id));
		return prefix+"empl_edit";
	}
	
	@RequestMapping("empl_delete")
	@ResponseBody
	public Object emplDelete(int id){
		emplService.emplDelete(id);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	
}
